package DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import Beans.Agent;
import Beans.Express;
import Utils.HibernateUtil;

public class ExpressDao {
	private SessionFactory sessionFactory;

	public ExpressDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	
	/**
	 * 删除快递网点
	 */
	public void delete(Express express) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(express);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改快递网点
	 */
	public void edit(Express express) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(express);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存新增的快递网点
	 */
	public void save(Express express) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(express);
		tr.commit();
		session.close();
	}
	
	/**
	 * 充值缴费
	 */
	public void payIn(Express expressBranch){
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(expressBranch);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回指定网点名称和公司id返回网点
	 */
	public List<Express> getExpressBranchByNC(int company, int branch,String expressBranch){
		
		CompanyDao companyDao =new CompanyDao();
		BranchDao branchDao =new BranchDao();
		
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Express.class);
		c.add(Restrictions.eq("company",companyDao.getCompanyById(company)));
		c.add(Restrictions.eq("branch",branchDao.getBranchById(branch)));
		c.add(Restrictions.eq("expressName", expressBranch));
		List<Express> expressBranchs=c.list();
		tr.commit();
		session.close();
		return expressBranchs;
	}
	
	/**
	 * 返回快递网点
	 */
	public Express getExpressBranch(int expressId){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Express express=(Express)session.get(Express.class, expressId);
		tr.commit();
		session.close();
		return express;
	}
	/**
	 * 启用/停用网点
	 * @param expressId
	 * @param status
	 * @return
	 */
	public Express changeStatus(int expressId,int status){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Express express=(Express)session.get(Express.class, expressId);
		express.setStatus(status);
		tr.commit();
		session.close();
		return express;
	}
	
	/**
	 * 返回所有快递网点信息
	 */
	public List<Express> getAllExpress(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Express.class);
		List<Express> expressBranchs=c.list();
		tr.commit();
		session.close();
		return expressBranchs;
	}
	/**
	 * 获取代理商网点
	 * @return
	 */
	public List<Express> getAgentExpress(Agent agent){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Express.class);
		c.add(Restrictions.eq("agent",agent));
		List<Express> expressBranchs=c.list();
		tr.commit();
		session.close();
		return expressBranchs;
	}
	
	/**
	 * 返回某个公司的快递网点
	 */
	public List<Express> getCompanyExpressBranch(int company,Agent agent){
		
		CompanyDao companyDao = new CompanyDao();
		
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Express.class);
		c.add(Restrictions.eq("company",companyDao.getCompanyById(company)));
//		c.add(Restrictions.eq("agent",agent));
		c.add(Restrictions.eq("status",0));
		List<Express> expressBranchs=c.list();
		tr.commit();
		session.close();
		return expressBranchs;
	}
	
	/**
	 * 返回某个公司的快递网点
	 */
	public List<Express> getCompanyExpressBranch(int company){
		
		CompanyDao companyDao = new CompanyDao();
		
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Express.class);
		c.add(Restrictions.eq("company",companyDao.getCompanyById(company)));
		List<Express> expressBranchs=c.list();
		tr.commit();
		session.close();
		return expressBranchs;
	}

	/**
	 * 通过网点ID返回指定快递网点信息
	 * 
	 * @return
	 */
	public Express getExpressBranchById(int expressBranchId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Express expressBranch = (Express) session.get(Express.class, expressBranchId);
		tr.commit();
		session.close();
		return expressBranch;
	}
}
